<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <!--      <el-form-item label="出库单号" prop="danhao">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.danhao"-->
      <!--          placeholder="请输入出库单号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="出库类型" prop="chukuType">-->
      <!--        <el-select v-model="queryParams.chukuType" placeholder="请选择出库类型" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.chuku_type"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="订单类型" prop="orderType">-->
      <!--        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.order_type"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="销售订单编号" prop="orderDanhao">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.orderDanhao"-->
      <!--          placeholder="请输入销售订单编号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="合同编号" prop="contractCode">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.contractCode"-->
      <!--          placeholder="请输入合同编号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="物料名称" prop="itemName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.itemName"-->
      <!--          placeholder="请输入物料名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="物料编码" prop="itemId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.itemId"-->
      <!--          placeholder="请输入物料编码"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="规格型号" prop="spec">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.spec"-->
      <!--          placeholder="请输入规格型号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="物料单位" prop="unit">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.unit"-->
      <!--          placeholder="请输入物料单位"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="库位编码" prop="kuweiCode">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.kuweiCode"-->
      <!--          placeholder="请输入库位编码"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="物料类型" prop="className">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.className"-->
      <!--          placeholder="请输入物料类型"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="出库日期" prop="chukuDate">-->
      <!--        <el-date-picker-->
      <!--          clearable-->
      <!--          v-model="queryParams.chukuDate"-->
      <!--          type="daterange"-->
      <!--          range-separator="至"-->
      <!--          start-placeholder="开始日期"-->
      <!--          end-placeholder="结束日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item>-->
      <!--        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
      <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      <!--      </el-form-item>-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          :disabled="multiple"-->
      <!--          v-hasPermi="['business:chukuInfo:addBill']"-->
      <!--        >生成开票通知单</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleScJs"
          v-hasPermi="['business:chukuJiesuan:add']"
        >生成结算单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleQxJs"
          v-hasPermi="['business:chukuJiesuan:add']"
        >取消结算单</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:chukuJiesuan:export']"
        >导出</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-s-unfold"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleAccount"-->
      <!--          v-hasPermi="['business:chukuInfo:edit']"-->
      <!--        >生成订单成本核算-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-s-unfold"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleSummary"-->
      <!--          v-hasPermi="['business:chukuInfo:edit']"-->
      <!--        >订单成本汇总-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table show-summary :summary-method="getSummaries" v-loading="loading" :data="chukuInfoList" @selection-change="handleSelectionChange">
      <el-table-column width="80" type="expand">
        <template slot-scope="props">
          <el-table :row-class-name="rowClassName" :data="props.row.records" show-summary :summary-method="getSummaries2">
            <!-- <el-table-column label="组织名称" align="center" prop="factoryName" />
            <el-table-column label="组织代码" align="center" prop="factoryCode" />
            <el-table-column label="结算月份" align="center" prop="month" /> -->
            <el-table-column label="领用科室" align="center" prop="sapProof">
              <template slot-scope="scope">
                <el-button type="text" @click="handleDetail(scope.row)">{{ scope.row.sapProof }}</el-button>
              </template>
            </el-table-column>
<!--            <el-table-column label="出库结算单号" align="center" prop="danhao">-->
<!--              <template slot-scope="scope">-->
<!--                <el-button type="text" @click="handleDetail(scope.row.danhao)">{{ scope.row.danhao }}</el-button>-->
<!--              </template>-->
<!--            </el-table-column>-->
            <el-table-column label="领用金额" align="center" prop="totalPrice">
              <template slot-scope="scope">
                {{(scope.row.totalPrice-0).toFixed(2)}}
              </template>
            </el-table-column>
            <el-table-column label="结算单号" align="center" prop="jiesuanDanhao" />
            <el-table-column label="核算日期" align="center" prop="createTime" />
            <el-table-column label="核算人" align="center" prop="createBy" />
            <el-table-column label="审核人" align="center" prop="updateBy" />
            <el-table-column fixed="right"  width="180" label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-circle-check"
                  @click="printJsd(scope.row)"
                >打印明细</el-button>
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>

      <el-table-column label="组织名称" align="center" prop="factoryName" />
      <el-table-column label="组织代码" align="center" prop="factoryCode" />
      <el-table-column label="结算月份" align="center" prop="month" />
      <el-table-column label="总金额" align="center" prop="totalPrice">
        <template slot-scope="props">
          {{props.row.totalPrice.toFixed(2)}}
        </template>
      </el-table-column>
      <el-table-column fixed="right"  width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="props">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="printJe(props.row)"
          >打印结算单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->

    <!-- 添加或修改出库单对话框 -->
    <!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
    <!--        <el-form-item label="开票日期" prop="date">-->
    <!--          <el-date-picker-->
    <!--            v-model="form.date"-->
    <!--            type="month"-->
    <!--            value-format="yyyy-MM"-->
    <!--            format="yyyy-MM"-->
    <!--            placeholder="请选择开票日期">-->
    <!--          </el-date-picker>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->

    <el-dialog
      title="领用单详情"
      :visible.sync="wlOpen"
      width="70%"
      :close-on-click-modal = false
      :close-on-press-escape = false
      :before-close="handleClose">
      <el-table :data="wlList" style="width: 100%" show-summary :summary-method="getSummaries">
        <el-table-column  type="index" width="80" label="序号"/>
        <el-table-column prop="itemName" label="物料名称"/>
        <el-table-column prop="itemCode" label="物料编号"/>
        <el-table-column prop="unit" label="物料单位"/>
        <el-table-column prop="ckNum" label="领用数量"/>
        <el-table-column prop="unitPrice" label="单价"/>
        <el-table-column prop="totalPrice" label="领用金额">
          <template slot-scope="scope">
            <span>{{(scope.row.totalPrice-0).toFixed(2)}}</span>
          </template>
        </el-table-column>>
        <el-table-column prop="remark" label="备注"/>
      </el-table>
    </el-dialog>
    <el-dialog
      :title="jsTitle"
      :visible.sync="jsOpen"
      width="40%"
      :close-on-click-modal = false
      :close-on-press-escape = false
      :before-close="handleCloseJs">
      <el-form :model="jsQuery" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="结算月份" prop="danhao">
          <el-date-picker
            v-model="jsQuery.jiesuanMonth"
            type="month"
            value-format="yyyy-MM"
            format="yyyy-MM"
            placeholder="选择月">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="subJs">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <div style="width: 100%;display: none">
      <div style="width: 100%;font-size: 20px;page-break-after: always;" ref="printck">
        <div class="table-container" style="width: 90%;margin:0 auto">
          <table class="assetPrintTable"  cellpadding="0" cellspacing="0"  style="margin-top: 10px;width: 94%;text-align: center;font-size: 12px">
            <thead  style="display:table-header-group;width: 90%;font-size: 12px;">
            <tr>
              <th class="title" style="text-align: center;font-weight: 450;font-size: 12px" colspan="9">章丘区第二人民医院</th>
            </tr>
            <tr>
              <th class="title" style="text-align: center" width="100%" colspan="9">{{formData.className}}领用结算单</th>
            </tr>
            <tr>
              <th class="titleItem" colspan="2">
                <div>领用科室：{{ formData.sapProof }}</div>
              </th>
              <th class="titleItem" colspan="4">
                <div>结算单号：{{formData.jiesuanDanhao}}</div>
              </th>
              <th class="titleItem" colspan="3">
                <div>结算日期：{{ formData.jiesuanMonth }}</div>
              </th>
            </tr>
            <tr style="font-size: 12px">
              <td class="tds" style="width: 5%;border-top: 1px solid black;border-left: 1px solid black">序号</td>
              <td class="tds" style="width: 20%;border-top: 1px solid black">出库单号</td>
              <td class="tds" style="width: 15%;border-top: 1px solid black">物料名称</td>
              <td class="tds" style="width: 10%;border-top: 1px solid black">物料编号</td>
              <td class="tds" style="width: 5%;border-top: 1px solid black">单位</td>
              <td class="tds" style="width: 5%;border-top: 1px solid black">数量</td>
              <td class="tds" style="width: 5%;border-top: 1px solid black">单价</td>
              <td class="tds" style="width: 5%;border-top: 1px solid black">总价</td>
              <td class="tds" style="width: 15%;border-top: 1px solid black">供应商</td>
            </tr>
            </thead>
            <tbody style="font-size: 12px;padding-top: 20px">
            <tr style="font-size: 16px" v-for="(item,index) in chukuInfoList2" :key="index">
              <td class="tds" style="width: 5%;border-left: 1px solid black">{{index+1}}</td>
              <td class="tds" style="width: 20%;overflow-wrap:anywhere;">{{item.danhao}}</td>
              <td class="tds" style="width: 15%;overflow-wrap:anywhere;">{{item.itemName}}</td>
              <td class="tds" style="width: 10%;overflow-wrap:anywhere;">{{item.itemCode}}</td>
              <td class="tds" style="width: 5%;overflow-wrap:anywhere;">{{item.unit}}</td>
              <td class="tds" style="width: 5%;overflow-wrap:anywhere;">{{item.ckNum}}</td>
              <td class="tds" style="width: 5%;overflow-wrap:anywhere;">{{(item.unitPrice-0).toFixed(2)}}</td>
              <td class="tds" style="width: 5%;overflow-wrap:anywhere;">{{(item.totalPrice-0).toFixed(2)}}</td>
              <td class="tds" style="width: 15%;overflow-wrap:anywhere;">{{item.supplyName}}</td>
            </tr>
            </tbody>
            <tfoot>
            <tr>
              <th class="titleItem" colspan="1"></th>
              <th class="titleItem" colspan="4">总计：</th>
              <th class="titleItem" colspan="2">{{ formData.ckNum }}</th>
              <th class="titleItem" colspan="2">{{ (formData.totalPrice-0).toFixed(2) }}</th>
            </tr>
            <tr>
              <th class="titleItem" colspan="1"></th>
              <th class="titleItem" colspan="4">保管：颜晓双</th>
              <th class="titleItem" colspan="4">制单：宋伟华</th>
            </tr>
            </tfoot>
          </table>
        </div>
      </div>
    </div>

    <div style="width: 100%;display: none">
      <div style="width: 100%;font-size: 20px;page-break-after: always;" ref="printmx">
        <div class="table-container" style="width: 90%;margin:0 auto">
          <table class="assetPrintTable"  cellpadding="0" cellspacing="0"  style="margin-top: 10px;width: 94%;text-align: center;font-size: 12px">
            <thead  style="display:table-header-group;width: 90%;font-size: 12px;">
            <tr>
              <th class="title" style="text-align: center;font-weight: 450;font-size: 12px" colspan="9">章丘区第二人民医院</th>
            </tr>
            <tr>
              <th class="title" style="text-align: center" width="100%" colspan="9">{{formData2.className}}领用结算单</th>
            </tr>
            <tr>
              <th class="titleItem" colspan="3">
                <div>仓库：物资仓库</div>
              </th>
<!--              <th class="titleItem" colspan="1">-->
<!--                <div>{{formData2.chukuDate}}</div>-->
<!--              </th>-->
              <th class="titleItem" colspan="2">
                <div style="text-align: center">结算月份：{{ formData2.month }}</div>
              </th>
            </tr>
            <tr style="font-size: 12px">
              <td class="tds" style="width: 10%;border-top: 1px solid black;border-left: 1px solid black">序号</td>
              <td class="tds" style="width: 20%;border-top: 1px solid black">领用科室</td>
              <td class="tds" style="width: 20%;border-top: 1px solid black">领用金额</td>
              <td class="tds" style="width: 30%;border-top: 1px solid black">结算单号</td>
              <td class="tds" style="width: 20%;border-top: 1px solid black">核算人</td>
<!--              <td class="tds" style="width: 15%;border-top: 1px solid black">审核人</td>-->
            </tr>
            </thead>
            <tbody style="font-size: 12px;padding-top: 20px">
            <tr style="font-size: 16px" v-for="(item,index) in jeList" :key="index">
              <td class="tds" style="width: 10%;border-left: 1px solid black">{{index+1}}</td>
              <td class="tds" style="width: 20%;overflow-wrap:anywhere;">{{item.sapProof}}</td>
              <td class="tds" style="width: 20%;overflow-wrap:anywhere;">{{(item.totalPrice-0).toFixed(2)}}</td>
              <td class="tds" style="width: 30%;overflow-wrap:anywhere;">{{item.jiesuanDanhao}}</td>
              <td class="tds" style="width: 20%;overflow-wrap:anywhere;">{{item.createBy}}</td>
<!--              <td class="tds" style="width: 15%;overflow-wrap:anywhere;">{{item.updateBy}}</td>-->
            </tr>
            </tbody>
            <tfoot>
            <tr>
              <th class="titleItem" colspan="2">总计：</th>
              <th class="titleItem" colspan="1" style="text-align: center">{{ (formData2.totalPrice-0).toFixed(2) }}</th>
              <th class="titleItem" colspan="2"></th>
            </tr>
            <tr>
              <th class="titleItem" colspan="1"></th>
              <th class="titleItem" colspan="2">保管：颜晓双</th>
              <th style="text-align: center" class="titleItem" colspan="2">制单：宋伟华</th>
            </tr>
            </tfoot>
          </table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { listChukuInfo,handleSummary,handleAccount,listJieSuanChukuInfo, getChukuInfo, delChukuInfo, addChukuInfo, updateChukuInfo } from "@/api/business/chukuInfo";
import { chukuJieSuanAddBill } from "@/api/business/bill";
import moment from "moment";
import {listRukuInfo} from "@/api/business/rukuInfo";
import { listCkjs,addCkjs, delCkjs, } from "@/api/business/ckjs";
import print from 'print-js';

export default {
  name: "ChukuInfoJieSuan",
  dicts: ['order_type', 'chuku_type'],
  data() {
    return {
      formData2:{},
      jeList:[],
      chukuInfoList2:[],
      formData:{},
      jsQuery:{

      },
      jsOpen:false,
      jsTitle:'',
      wlList:[],
      wlOpen:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出库单表格数据
      chukuInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10000,
        factoryName: null,
        factoryCode: null,
        danhao: null,
        chukuType: null,
        orderType: null,
        orderDanhao: null,
        contractCode: null,
        contractNum: null,
        jhckNum: null,
        ckNum: null,
        chukuDate: null,
        itemName: null,
        itemId: null,
        spec: null,
        unit: null,
        lot: null,
        supplyId: null,
        supplyName: null,
        stockId: null,
        stockName: null,
        kuquCode: null,
        kuquName: null,
        kuweiCode: null,
        itemCode: null,
        price: null,
        totalPrice: null,
        unitPrice: null,
        classId: null,
        className: null,
        isTuoguan: null,
        qualityState: null,
        makerCode: null,
        makerName: null,
        size: null,
        singleWeight: null,
        totalWeight: null,
        useState: null,
        color: null,
        figure: null,
        caizhi: null,
        txm: null,
        tpm: null,
        pstxm: null,
        psTime: null,
        zubie: null,
        pickingName: null,
        pickingSegment: null,
        pickingTime: null,
        txmPrintNum: null,
        ckPrintNum: null,
        utensilNo: null,
        sapProof: null,
        ckState: null,
        zzhxState: null,
        zzrkTime: null,
        zzrkRemark: null,
        zzrkName: null,
        zzrkNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        date: [
          { required: true, message: "开票日期不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    printJe(row){
      this.formData2 = row
      this.jeList = row.records;
      setTimeout(()=>{
        this.priDanMx()

      },100)
    },
    priDanMx(){

      const style = '@page { margin:0;padding-bottom:10px important; } ' +'@media print { .tds{border-bottom:1px solid black;text-align:center;border-right:1px solid black;font-size:14px}.title{font-size:20px;font-weight:500}.titleItem{text-align:left;font-size:14px;font-weight:500} }  ' ;
      printJS({
        printable:this.$refs.printmx,
        type:'html',
        header:'',
        targetStyles:['*'],
        scanStyles: false,
        style:style,
        onPrintDialogClose: () => {
          console.log('弹窗关闭');
        },
        onLoadingEnd:()=>{
          console.log('加载完毕')
        }
      })
    },
    printJsd(row){
      console.log(row)
      this.formData = row
      let obj={
        pageNum:1,
        pageSize:1000,
        jiesuanDanhao:row.jiesuanDanhao,
        sapProof:row.sapProof
      }
      listCkjs(obj).then(res=>{
        this.chukuInfoList2 = res.rows;
        setTimeout(()=>{
          this.priDanQt()

        },100)
        // this.wlOpen = true;
      })
      // this.chukuInfoList2 = row.records;

    },
    priDanQt(){

      const style = '@page { margin:0;padding-bottom:10px important; } ' +'@media print { .tds{border-bottom:1px solid black;text-align:center;border-right:1px solid black;font-size:14px}.title{font-size:20px;font-weight:500}.titleItem{text-align:left;font-size:14px;font-weight:500} }  ' ;
      printJS({
        printable:this.$refs.printck,
        type:'html',
        header:'',
        targetStyles:['*'],
        scanStyles: false,
        style:style,
        onPrintDialogClose: () => {
          console.log('弹窗关闭');
        },
        onLoadingEnd:()=>{
          console.log('加载完毕')
        }
      })
    },
    handleCloseJs(){
      this.jsOpen = false;
    },
    subJs(){
      if(!this.jsQuery.jiesuanMonth){
        this.$message.error('请选择结算月份')
      }else{
        if(this.jsQuery.isSc==1){
          this.jsQuery.qzType = 'LYJ'
          addCkjs(this.jsQuery).then(res=>{
            this.jsQuery = {}
            this.jsOpen = false
            this.getList()
          })

        }else if(this.jsQuery.isSc==2){
          let id = this.jsQuery.jiesuanMonth
          delCkjs(id).then(res=>{
            this.jsQuery = {}
            this.jsOpen = false
            this.getList()
          })

        }
      }

    },
    handleScJs(){
      this.jsOpen = true;
      this.jsTitle='生成结算单'
      this.jsQuery.isSc = 1
    },
    handleQxJs(){
      this.jsOpen = true;
      this.jsTitle='取消结算单'
      this.jsQuery.isSc = 2
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        if (index === 1||index === 2||index === 3||index === 5||index === 7) {
          sums[index] = '';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return this.keepTwoDecimalFull(Number(prev) + curr);
            } else {
              return prev;
            }
          }, 0);
          sums[index] += '';
        } else {
          sums[index] = '';
        }
      });

      return sums;
    },
    getSummaries2(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        if (index === 2||index === 3||index === 4||index === 5) {
          sums[index] = '';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return this.keepTwoDecimalFull(Number(prev) + curr);
            } else {
              return prev;
            }
          }, 0);
          sums[index] += '';
        } else {
          sums[index] = '';
        }
      });

      return sums;
    },
    rowClassName(row, rowIndex){
      if (rowIndex % 2 === 1) {
        return '';
      }
      return 'custom-row';
    },
    handleClose(){
      this.wlOpen = false;
    },
    handleDetail(e){
      let obj={
        pageNum:1,
        pageSize:1000,
        receivingUnit:e.receivingUnit,
        jiesuanDanhao:e.jiesuanDanhao
      }
      listCkjs(obj).then(res=>{
        this.wlList = res.rows;
        this.wlOpen = true;
      })

    },
    keepTwoDecimalFull(num) {
      var result = parseFloat(num);
      if (isNaN(result)) {
        alert('传递参数错误，请检查！');
        return false;
      }
      result = Math.round(num * 100) / 100;
      var s_x = result.toString(); //将数字转换为字符串

      var pos_decimal = s_x.indexOf('.'); //小数点的索引值


      // 当整数时，pos_decimal=-1 自动补0
      if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
      }

      // 当数字的长度< 小数点索引+2时，补0
      while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
      }
      return s_x;
    },
    /** 查询出库单列表 */
    getList() {
      this.loading = true;
      listCkjs(this.queryParams).then(response => {
        let obj = response.rows;
        let monthlySummary = [];

        obj.forEach(record => {
          const month = record.chukuDate.substring(0, 7);

          // 检查这个月份是否已经存在于汇总数组中
          let foundMonth = monthlySummary.find(item => item.month === month);

          if (foundMonth) {
            // 如果找到了，就更新这个月份的总数量和总价格
            foundMonth.totalQuantity += record.ckNum;
            foundMonth.totalPrice += record.totalPrice;
            let foundRecord = foundMonth.records.find(r => r.receivingUnit === record.receivingUnit);

            if (foundRecord) {
              // 如果有相同的danhao，合并数量和总价
              foundRecord.ckNum += record.ckNum;
              foundRecord.totalPrice += record.totalPrice;
            } else {
              // 如果没有相同的danhao，将记录添加到数组中
              foundMonth.records.push(record);
            }
          } else {
            // 如果没找到，就创建一个新的条目并添加到汇总数组中
            monthlySummary.push({
              month: month,
              factoryName:record.factoryName,
              factoryCode:record.factoryCode,
              totalQuantity: record.ckNum,
              totalPrice: record.totalPrice,
              records: [record] // 初始化详细记录数组
            });
          }
        });
        this.chukuInfoList = monthlySummary;
        // this.chukuInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        factoryName: null,
        factoryCode: null,
        danhao: null,
        chukuType: null,
        orderType: null,
        orderDanhao: null,
        contractCode: null,
        contractNum: null,
        jhckNum: null,
        ckNum: null,
        chukuDate: null,
        itemName: null,
        itemId: null,
        spec: null,
        unit: null,
        lot: null,
        supplyId: null,
        supplyName: null,
        stockId: null,
        stockName: null,
        kuquCode: null,
        kuquName: null,
        kuweiCode: null,
        itemCode: null,
        price: null,
        totalPrice: null,
        unitPrice: null,
        classId: null,
        className: null,
        isTuoguan: null,
        qualityState: null,
        makerCode: null,
        makerName: null,
        size: null,
        singleWeight: null,
        totalWeight: null,
        useState: null,
        color: null,
        figure: null,
        caizhi: null,
        txm: null,
        tpm: null,
        pstxm: null,
        psTime: null,
        zubie: null,
        pickingName: null,
        pickingSegment: null,
        pickingTime: null,
        txmPrintNum: null,
        ckPrintNum: null,
        utensilNo: null,
        sapProof: null,
        ckState: null,
        zzhxState: null,
        zzrkTime: null,
        zzrkRemark: null,
        zzrkName: null,
        zzrkNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "领用结算生成开票通知单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChukuInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出库单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateChukuInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.ids=this.ids;
            chukuJieSuanAddBill(this.form).then(response => {
              this.$modal.alertSuccess("生成开票通知单成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除出库单编号为"' + ids + '"的数据项？').then(function() {
        return delChukuInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 生成开票通知单 */
    handleAddBill(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确定要生成开票通知单？').then(function() {
        return chukuJieSuanAddBill(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("生成成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/chukuInfo/export', {
        ...this.queryParams
      }, `chukuInfo_${new Date().getTime()}.xlsx`)
    },
    handleAccount(row){
      const id = row.id || this.ids
      handleAccount(id).then(response => {
        this.$modal.msgSuccess("核算成功");
      });
    },
    handleSummary(row){
      const id = row.id || this.ids
      handleSummary(id).then(response => {
        this.$modal.msgSuccess("汇总成功");
      });
    },
  }
};
</script>
